<?php
namespace Admin\Model;
use Think\Model;
class MatchModel extends Model {
	public $table='zjy_match';
	public $page; //当前页
	public $perpage; //每页显示
	public $match_id; //ID
	public $pc_id; //分类ID
	public $post; //提交的信息 数组
	public $ids; //IDs 主要用于删除
	public $sorts; //IDs 主要用于删除
	public $keyword; //关键词
	public $pc_ids; //获取分类ID下的子分类

	//提交
	public function match_add_post(){
		$Model=M("match");
		$data=$this->post;
		if($Model->add($data)){
			return true;
		}else{
			return false;
		}
	}
	public function get_match_list()
	{
		$page=$this->page;
		$perpage=$this->perpage;
		//搜索
		$where='where 1 and match_state_id<>1';
		if($this->keyword){
			$where.="and `match_sn` LIKE '%".$this->keyword."%'";
		}
		/*
		if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and c.pc_id in(".$pc_ids.")";
		}*/
		$start=($page-1)*$perpage;
		$Model=new Model();
		$Member=M("Member");
		$ProductClass=M("ProductClass");
		$Brand=M("Brand");
		$Spec=M("Spec");
		$Unit=M("Unit");
		$Purchase=M("Purchase");
		$Supply=M("Supply");
		$MatchState=M("MatchState");
		$sql="select * from ".$this->table." ".$where." order by match_sort asc,match_id desc limit $start,$perpage";
		$list=$Model->query($sql);
		//print_r($list[3]);exit;
		foreach($list as $k=>$v){
			//采购信息
			$purchase_info=$Purchase->where('purchase_id='.$v['purchase_id'])->find();
			//print_r();exit;
			//echo $pinfo['member_id'].'<br />';
			$list[$k]['purchase_info']['member_truename']=$Member->where("member_id=".intval($purchase_info['member_id']))->getField('member_truename');
			$list[$k]['purchase_info']['member_company']=$Member->where('member_id='.intval($purchase_info['member_id']))->getField('member_company');
			$list[$k]['purchase_info']['pc_name']=$ProductClass->where('pc_id='.intval($purchase_info['pc_id']))->getField('pc_name');
			$list[$k]['purchase_info']['brand_name']=$Brand->where('brand_id='.intval($purchase_info['brand_id']))->getField('brand_name');
			$list[$k]['purchase_info']['spec_name']=$Spec->where('spec_id='.intval($purchase_info['spec_id']))->getField('spec_name');
			$list[$k]['purchase_info']['unit_name']=$Unit->where('unit_id='.intval($purchase_info['unit_id']))->getField('unit_name');
			//供货信息
			$supply_info=$Supply->where('supply_id='.$v['supply_id'])->find();
			//echo $supply_info['member_id'].'<br />';
			$list[$k]['supply_info']['member_truename']=$Member->where("member_id=".intval($supply_info['member_id']))->getField('member_truename');
			$list[$k]['supply_info']['member_company']=$Member->where('member_id='.intval($supply_info['member_id']))->getField('member_company');
			$list[$k]['supply_info']['pc_name']=$ProductClass->where('pc_id='.intval($supply_info['pc_id']))->getField('pc_name');
			$list[$k]['supply_info']['brand_name']=$Brand->where('brand_id='.intval($supply_info['brand_id']))->getField('brand_name');
			$list[$k]['supply_info']['spec_name']=$Spec->where('spec_id='.intval($supply_info['spec_id']))->getField('spec_name');
			$list[$k]['supply_info']['unit_name']=$Unit->where('unit_id='.intval($supply_info['unit_id']))->getField('unit_name');
			$list[$k]['match_state_name']=$MatchState->where('match_state_id='.$v['match_state_id'])->getField('match_state_name');
		}
		//print_r($list);exit;
		return $list;
	}
	//获得总数
	public function get_match_total()
	{
		//搜索
		$where='where 1 and match_state_id<>1';
		if($this->keyword){
			$where.="and `match_sn` LIKE '%".$this->keyword."%'";
		}
		/*if($this->pc_id){
			$pc_ids=$this->get_sub_pc_id($this->pc_id);
			$pc_ids=mb_strcut($pc_ids,1); //裁掉第一个，
			$where.="and pc_id in(".$pc_ids.")";
		}*/
		$Model=new Model();
		$sql="select count(match_id) as total from ".$this->table." ".$where;
		$query=$Model->query($sql);
		return $query[0]['total'];
	}
	//获取信息
	public function get_match_info()
	{
		$match_id=intval($this->match_id);
		$Model=M("Match");
		$Purchase=M("Purchase");
		$Supply=M("Supply");
		$MatchState=M("MatchState");
		$Member=M("Member");
		$ProductClass=M("ProductClass");
		$Brand=M("Brand");
		$Spec=M("Spec");
		$Unit=M("Unit");
		$info=$Model->where('match_id='.$match_id)->find();
		$info['unit_name']=$Unit->where('unit_id='.intval($info['unit_id']))->getField('unit_name');
		//采购信息
		$info['purchase_info']=$Purchase->where('purchase_id='.$info['purchase_id'])->find();
		$info['purchase_info']['member_truename']=$Member->where('member_id='.intval($info['purchase_info']['member_id']))->getField('member_truename');
		$info['purchase_info']['member_company']=$Member->where('member_id='.intval($info['purchase_info']['member_id']))->getField('member_company');
		$info['purchase_info']['pc_name']=$ProductClass->where('pc_id='.intval($info['purchase_info']['pc_id']))->getField('pc_name');
		$info['purchase_info']['brand_name']=$Brand->where('brand_id='.intval($info['purchase_info']['brand_id']))->getField('brand_name');
		$info['purchase_info']['spec_name']=$Spec->where('spec_id='.intval($info['purchase_info']['spec_id']))->getField('spec_name');
		$info['purchase_info']['unit_name']=$Unit->where('unit_id='.intval($info['purchase_info']['unit_id']))->getField('unit_name');
		//供货信息
		$info['supply_info']=$Supply->where('supply_id='.$info['supply_id'])->find();
		$info['supply_info']['member_truename']=$Member->where('member_id='.intval($info['supply_info']['member_id']))->getField('member_truename');
		$info['supply_info']['member_company']=$Member->where('member_id='.intval($info['supply_info']['member_id']))->getField('member_company');
		$info['supply_info']['pc_name']=$ProductClass->where('pc_id='.intval($info['supply_info']['pc_id']))->getField('pc_name');
		$info['supply_info']['brand_name']=$Brand->where('brand_id='.intval($info['supply_info']['brand_id']))->getField('brand_name');
		$info['supply_info']['spec_name']=$Spec->where('spec_id='.intval($info['supply_info']['spec_id']))->getField('spec_name');
		$info['supply_info']['unit_name']=$Unit->where('unit_id='.intval($info['supply_info']['unit_id']))->getField('unit_name');
		//print_r($info);exit;
		return $info;
	}
	//编辑提交
	public function match_edit_post()
	{
		$Model=M("Match");
		$data=$this->post;
		$match_id=intval($this->post['match_id']);
		$where['match_id']=$match_id;
		$result=$Model->where($where)->save($data);
		if($result === false) {
			return false;
		}else{
			return true;
		}
	}
	//删除
	public function delete_match()
	{
		$ids=$this->ids;
		$Model=M("Match");
		foreach($ids as $v){
			$condition['match_id']=$v;
			if(!$Model->where($condition)->delete()){
				return false;
			}
		}
		return true;
	}
	//批量更新排序
	public function update_match_sort()
	{
		$ids=$this->ids;
		$sorts=$this->sorts;
		$Model=M("Match");
		foreach($ids as $k=>$v){
			$data['match_sort']=$sorts[$k];
			$where['match_id']=$v;
			$result=$Model->where($where)->save($data);
			if($result === false) {
				return false;
			}
		}
		return true;
	}

}